package com.zyd.shiro.controller.admin;

import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.QiniuUtil;
import com.zyd.shiro.util.ResultUtil;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    QiniuUtil qiniuUtil;

    @PostMapping("/")
    public ResponseVO add(MultipartFile file) {
        try {
            String name = qiniuUtil.putFile(file.getInputStream());
            String url = qiniuUtil.getFile(name);
            Map<String, String> map = new HashMap<>();
            map.put("id", name);
            map.put("url", url);
            return ResultUtil.success("图片上传成功", map);
        } catch (IOException e) {
            return ResultUtil.error(e.getMessage());
        }
    }

    @DeleteMapping("/")
    public ResponseVO delete(String id) {
        return ResultUtil.success("图片删除成功！");
    }
}
